import useQuery from '@/api/query'
import updateTextList from '@/utils/updateTextList'
import moment from 'moment';
import xuanRanTextList from '@/utils/xuanRanTextList';
// moment(dateStr).format('YYYY年MM月DD日 dddd')


// 获取数据
export const useTextList = async (cate_id,state) => {
  // console.log('准备发送获取文章内容接口')
  const res = await useQuery.get('https://big-event-vue-api-t.itheima.net/my/article/list',{
    params: {
      pagenum: 1,
      pagesize: 20,
      cate_id,
      state
    }
  })
  // console.log('获取成功了',res.data.data)
  res.data.data.forEach( item => {
    // 让时间显示的更稳定
    const isoDate = new Date(item.pub_date).toISOString()
    item.pub_date = moment(isoDate).format('YYYY年MM月DD日')
  });
  return res.data.data
}
export const useTextList1 = async (cate_id) => {
  // console.log('准备发送获取文章内容接口')
  const res = await useQuery.get('https://big-event-vue-api-t.itheima.net/my/article/list',{
    params: {
      pagenum: 1,
      pagesize: 20,
      cate_id
    }
  })
  // console.log('获取成功了',res.data.data)
  res.data.data.forEach( item => {
    // 让时间显示的更稳定
    const isoDate = new Date(item.pub_date).toISOString()
    item.pub_date = moment(isoDate).format('YYYY年MM月DD日')
  });
  return res.data.data
}
export const useTextList2 = async (state) => {
  // console.log('准备发送获取文章内容接口')
  const res = await useQuery.get('https://big-event-vue-api-t.itheima.net/my/article/list',{
    params: {
      pagenum: 1,
      pagesize: 20,
      state
    }
  })
  // console.log('获取成功了',res.data.data)
  res.data.data.forEach( item => {
    // 让时间显示的更稳定
    const isoDate = new Date(item.pub_date).toISOString()
    item.pub_date = moment(isoDate).format('YYYY年MM月DD日')
  });
  return res.data.data
}

// 发布文章
export const useAddText = async (submitList) => {
  // console.log('准备发送添加文章内容接口',submitList)
  await useQuery.post('https://big-event-vue-api-t.itheima.net/my/article/add',submitList)
  // console.log('发送添加文章成功',res)
  // xuanRanTextList()
  return 
}

// 修改文章
export const useUpdateText = async (submitList) => {
  // console.log('准备发送修改文章内容接口',submitList)
  await useQuery.put('https://big-event-vue-api-t.itheima.net/my/article/info',submitList)
  // console.log('发送成功')
  // const arr = await xuanRanTextList()
  return 
}

// 查看文章详细信息
export const useAllText = async (id) => {
  // console.log('准备发送修改文章内容接口',submitList)
  const res = await useQuery.get('https://big-event-vue-api-t.itheima.net/my/article/info',{
    params: {
      id,
    }
  })
  // console.log('发送成功')
  return res.data.data
}

// 删除文章、
export const useDelText = async (id) => {
  // console.log('准备发送修改文章内容接口',submitList)
  const res = await useQuery.delete('https://big-event-vue-api-t.itheima.net/my/article/info',{
    params: {
      id
    }
  })
  // console.log('删除成功')
  xuanRanTextList()
  return res.data.data
}

